package com.jrx.anytxn.param.controller.rights;


import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.entity.PrActivityInfoReq;
import com.jrx.anytxn.param.entity.PrActivityInfoRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.rights.IPrActivityInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 活动类型相关
 */
@Api(tags = "权益活动API接口")
@RestController
@RequestMapping("/param/activity")
public class PrActivityInfoController {

    private static final Logger logger = LoggerFactory.getLogger(PrPromotionInfoController.class);

    @Autowired
    private IPrActivityInfoService iPrActivityInfoService;

    @Autowired
    private InfoService infoService;


    @PostMapping("/")
    public TxnRespResult<PrActivityInfoRes> add(@RequestBody PrActivityInfoReq prActivityInfoReq) {
        TxnRespResult<PrActivityInfoRes> result = new TxnRespResult<>();

        prActivityInfoReq.setLogoImg("1");
        prActivityInfoReq.setCreateTime(new Date());
        prActivityInfoReq.setUpdateTime(new Date());
        PrActivityInfoRes res;
        try{
            prActivityInfoReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));

            String activityId = prActivityInfoReq.getActivityId();
            if (StringUtils.isBlank(activityId)) {
                activityId =  System.currentTimeMillis() + "";
                activityId = activityId + (int)((Math.random() * 9 + 1) * 10000);
            }
            logger.info("添加活动记录, activityId: {}", activityId);
            prActivityInfoReq.setActivityId(activityId);

            prActivityInfoReq.setCreateBy(infoService.getUser().getUserName());
            res = iPrActivityInfoService.addPrActivityInfoRes(prActivityInfoReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "更新活动参数", notes = "更新活动参数")
    @PutMapping("/")
    public TxnRespResult<PrActivityInfoRes> modifyPrActivityInfo(@RequestBody PrActivityInfoReq req) {
        TxnRespResult<PrActivityInfoRes> result = new TxnRespResult<>();
        PrActivityInfoRes res;
        try {
            req.setUpdateBy(infoService.getUser().getUserName());
            req.setUpdateTime(new Date());
            res = iPrActivityInfoService.modifyPrActivityInfo(req);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "根据租户ID和活动ID查询", notes = "根据租户ID和权益ID查询")
    @GetMapping(value = "/{tenantId}/{activityId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "activityId", value = "活动ID", required = true)
    })
    public TxnRespResult<PrActivityInfoRes> getById(@PathVariable String tenantId,
                                                          @PathVariable String activityId) {
        TxnRespResult<PrActivityInfoRes> result = new TxnRespResult<>();
        PrActivityInfoRes res;

        try {
            res = iPrActivityInfoService.getByTenIdAndActivityId(tenantId, activityId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "分页查询权益参数", notes = "分页查询权益参数")
    @PostMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PrActivityInfoRes>> getAllPageByTenId(@PathVariable("pageNum") Integer pageNum,
                                                                       @PathVariable("pageSize") Integer pageSize,
                                                                       @RequestBody PrActivityInfoReq req) {
        TxnRespResult<TxnPage<PrActivityInfoRes>> result = new TxnRespResult<>();
        try {
            req.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            TxnPage<PrActivityInfoRes> res = iPrActivityInfoService.findAllPageByCondition(pageNum, pageSize, req);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }



    @ApiOperation(value = "根据租户ID和活动ID删除信息", notes = "根据租户ID和活动ID删除信息")
    @DeleteMapping(value = "/{tenantId}/{activityId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "activityId", value = "权益ID", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndRightsId(@PathVariable String tenantId, @PathVariable String activityId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        try {
            DeleteLog deleteLog = new DeleteLog("admin");
            DeleteLogHelper.setDelleteHistory(deleteLog);
            Boolean flag =
                    iPrActivityInfoService.removeByTenantIdAndRightsId(tenantId, activityId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }



}
